//
//  EditView.m
//  项目三
//
//  Created by rimi1 on 2016/12/11.
//  Copyright © 2016年 Wangxingqian. All rights reserved.
//

#import "EditView.h"

static NSInteger spacing_ = 20;

@interface EditView () <UITextFieldDelegate, UITextViewDelegate>

@end

@implementation EditView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.name];
        [self addSubview:self.food];
        [self addSubview:self.stepLabel];
        [self addSubview:self.step];
        [self addSubview:self.img];
    }
    return self;
}

- (UITextField *)textFieldWithRect:(CGRect)rect placeholder:(NSString *)placeholder {
    UITextField *text = [[UITextField alloc] initWithFrame:rect];
    text.placeholder = placeholder;
    text.borderStyle = UITextBorderStyleBezel;
    text.clearButtonMode = UITextFieldViewModeWhileEditing;
    text.backgroundColor = [UIColor whiteColor];
    
    return text;
}

#pragma mark - UITextFieldDelegate

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString *str = textField.text;
    if ((([textField isEqual:self.name] && str.length >= 20) || ([textField isEqual:self.food] && str.length >= 40)) && string.length != 0) {
        return NO;
    }else {
        return YES;
    }
}

#pragma mark - UITextViewDelegate

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if (textView.text.length >= 350 && text.length != 0) {
        return NO;
    }else {
        return YES;
    }
}

#pragma mark - get

- (UITextField *)name {
    
    if (!_name) {
        
        _name = [self textFieldWithRect:CGRectMake(spacing_/2, spacing_, self.bounds.size.width - spacing_, 44) placeholder:@"菜谱名称"];
        _name.delegate = self;
        
    }
    return _name;
}

- (UITextField *)food {
    
    if (!_food) {
        
        _food = [self textFieldWithRect:CGRectMake(CGRectGetMinX(self.name.frame), CGRectGetMaxY(self.name.frame) + spacing_/2, CGRectGetWidth(self.name.bounds), CGRectGetHeight(self.name.bounds)) placeholder:@"所需食材"];
        _food.delegate = self;
        
    }
    return _food;
}

- (UILabel *)stepLabel {
    
    if (!_stepLabel) {
        
        CGFloat height = 14;
        _stepLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.food.frame), CGRectGetMaxY(self.food.frame) + spacing_/2, CGRectGetWidth(self.food.bounds), height)];
        _stepLabel.text = @"步骤和心得";
        _stepLabel.font = [UIFont systemFontOfSize:height];
        
    }
    return _stepLabel;
}

- (UITextView *)step {
    
    if (!_step) {
        
        _step = [[UITextView alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.stepLabel.frame), CGRectGetMaxY(self.stepLabel.frame) + spacing_/4, CGRectGetWidth(self.stepLabel.bounds), WXQAdaptionHeight(200))];
        _step.delegate = self;
        _step.font = [UIFont systemFontOfSize:18];
        
    }
    return _step;
}

- (UIButton *)img {
    
    if (!_img) {
        
        CGFloat diameter = WXQAdaptionHeight(60);
        _img = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.step.frame), CGRectGetMaxY(self.step.frame) + spacing_, diameter, diameter)];
        [_img setBackgroundImage:[UIImage imageNamed:@"加"] forState:UIControlStateNormal];
        
    }
    return _img;
}

@end















